package com.musiclibrary.scraper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.musiclibrary.common.JsoupUtils;
import com.musiclibrary.persistence.MusicAlbums;
import com.musiclibrary.persistence.MusicCategory;

public class MarathiMusicCategoryParser {

	public MusicCategory parseMusicCategory(Element element,WebPage<Map<String,String>> webPage){
		MusicCategory musicCategory=null;
		try{
			if(element!=null){
				musicCategory=new MusicCategory();
				Elements anchorElements = element.select("a");
				Element anchor = anchorElements.first();
				
				String categoryName = anchor.text();
				String categoryUrl = anchor.attr("href");
				
				musicCategory.setCategoryName(categoryName);
				musicCategory.setCategoryUrl(categoryUrl);
				
				//NOTE : Read 1st page default
				String[] parents = categoryUrl.split("/");
				String parentString = parents[2];
				//retrieve music albums from category url
				
				//update HtmlForm
				//update form action url
				webPage.getHtmlForm().setActionUrl("category/list");
				//update form parameter
				Map<String,String> formParameterMap =new HashMap<String, String>();
				formParameterMap.put("parent", parentString);
				formParameterMap.put("sort", "default");
				formParameterMap.put("page", "1");
				formParameterMap.put("commit", "Go%C2%BB");
				webPage.getFormHandler().updateForm(formParameterMap);
				//update HtmlForm END
				
				Document document = new JsoupUtils().loadPage(webPage);
				List<Element> albumElementList=new ArrayList<Element>();
				if(document!=null){
					//read music album elements
					Elements elements = document.select("body div#mainDiv div#cateogry div.catList div.catRow");
					if(elements!=null && elements.size()>0){
						albumElementList.addAll(elements);
					}
					
					//read other page count
					int pageStart=0;
					int pageEnd=0;
					Elements pageElements = document.select("body div#mainDiv div.pgn");
					Element page = pageElements.first();
					if (page!=null) {
						String pageString = page.text();
						if (pageString.contains("(") && pageString.contains(")")) {
							String substring = pageString.substring(pageString.indexOf("(") + 1, pageString.indexOf(")"));
							String[] split = substring.split("/");
							try {
								pageStart = Integer.parseInt(split[0].trim());
								pageEnd = Integer.parseInt(split[1].trim());
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					//read other album pages
					if(pageEnd>1){
						// 1st page already read
						pageStart++;
					
						for(;pageStart<=pageEnd ;pageStart++){
							Elements musicAlbumElements = getMusicAlbumElements(pageStart, webPage,parentString);
							if (musicAlbumElements!=null && musicAlbumElements.size()>0) {
								albumElementList.addAll(musicAlbumElements);
							}
						}
					}
				}

				//construct music album list from element
				MarathiMusicAlbumParser musicAlbumParser= new MarathiMusicAlbumParser();
				if(albumElementList.size()>0){
					List<MusicAlbums> musicAlbumList=new ArrayList<MusicAlbums>();
					for(Element _element: albumElementList){
						MusicAlbums musicAlbum = musicAlbumParser.constructMusicAlbum(_element, webPage);
						musicAlbum.setMusicCategory(musicCategory);
						musicAlbumList.add(musicAlbum);
					}
					musicCategory.setMusicAlbums(musicAlbumList);
				}
				//construct music album list END
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return musicCategory;
	}
	
	private Elements getMusicAlbumElements(int pageNo,WebPage<Map<String,String>> webPage, String parentString){
		Elements elements=null;
		try{
			//update HtmlForm
			//update form action url
			webPage.getHtmlForm().setActionUrl("category/list");
			//update form parameter
			Map<String,String> formParameterMap =new HashMap<String, String>();
			formParameterMap.put("parent", parentString);
			formParameterMap.put("sort", "default");
			formParameterMap.put("page", Integer.toString(pageNo));
			formParameterMap.put("commit", "Go%C2%BB");
			webPage.getFormHandler().updateForm(formParameterMap);
			//update HtmlForm END
			Document document = new JsoupUtils().loadPage(webPage);
			if(document!=null){
				elements = document.select("body div#mainDiv div#cateogry div.catList div.catRow");
			}			
		}catch(Exception e){
			e.printStackTrace();
		}
		return elements;
	} 
}
